package com.badlogic.androidgames.mypirates.Sprite;

import java.util.LinkedList;
import java.util.Queue;

import android.graphics.Bitmap;
import android.graphics.Point;
import android.graphics.Rect;

import com.badlogic.androidgames.framework.Game;
import com.badlogic.androidgames.framework.Graphics;
import com.badlogic.androidgames.mypirates.Constants;
import com.badlogic.androidgames.mypirates.MyPirateGame;
import com.badlogic.androidgames.mypirates.Sprite.Ship.MainSailDirections;

public class Cannon{
	public static int attackRange = 3;
	
	Ship myBoss;
	
	Game currentGame;
	
	Bitmap Texture;
	Bitmap explodeTexture;
	Bitmap splashTexture;
	
	public Animation lowerExplodeAnimation;
	public Animation upperExplodeAnimation;
	
	public Point p1;
	public Point p2;
	
	public boolean is1Exploded;
	public boolean is2Exploded;
	public boolean isInitialized;
	
	public Queue<Rect> q1 = new LinkedList<Rect>();
	public Queue<Rect> q2 = new LinkedList<Rect>();
	
	public boolean isAnimating(){
		return (lowerExplodeAnimation != null && lowerExplodeAnimation.IsActive)
				|| (upperExplodeAnimation != null && upperExplodeAnimation.IsActive)
				|| q1.size() != 0
				|| q2.size() != 0
				|| !isInitialized;
	}
	
	public Cannon(Game game, Ship boss) {
		myBoss = boss;
		currentGame = (MyPirateGame)game;
		isInitialized = false;
	}
	
	public void Initialize(){
		Graphics g = currentGame.getGraphics();
		Texture = g.newPixmap("cannon.png", null);
		explodeTexture = g.newPixmap("cannonblast.png", null);
		splashTexture = g.newPixmap("splash.png", null);
	}
	
	void makeMovingAnimation(Point p, Queue<Rect> q){
		int x = myBoss.x;
		int y = myBoss.y;
		if(myBoss.mainSailDirection == MainSailDirections.West || myBoss.mainSailDirection == MainSailDirections.East){
			if(p.x != -1){
				int totalStepPixel = (int)(Math.abs(p.y - y) / 3.0f * Constants.MapPieceHeight);
				
				int x1 = Constants.InitialPositionX + x * Constants.MapPieceWidth + Constants.CannonOffsetX;
				int y1;
				
				if(p.y < y)
					for(int i = 0; i < totalStepPixel; i++){
						y1 = Constants.InitialPositionY + y * Constants.MapPieceHeight - i * 3 + Constants.MapPieceHeight / 2;
						q.add(new Rect(x1, y1, x1 + 10, y1 + 10));
					}
				else
					for(int i = 0; i < totalStepPixel - 1; i++){
						y1 = Constants.InitialPositionY + y * Constants.MapPieceHeight + i * 3 + Constants.MapPieceHeight / 2;
						q.add(new Rect(x1, y1, x1 + 10, y1 + 10));
					}
			}
		}
		else{
			if(p.y != -1){
				int totalStepPixel = (int)(Math.abs(p.x - x) / 3.0f * Constants.MapPieceWidth);
				int x1;
				int y1 = Constants.InitialPositionY + y * Constants.MapPieceHeight + Constants.CannonOffsetY;
				
				if(p.x < x){
					for(int i = 0; i < totalStepPixel; i++){
						x1 = Constants.InitialPositionX + x * Constants.MapPieceWidth - i * 3 + Constants.MapPieceWidth / 2;
						q.add(new Rect(x1, y1, x1 + 10, y1 + 10));
					}
				}
				else{
					for(int i = 0; i < totalStepPixel - 1; i++){
						x1 = Constants.InitialPositionX + x * Constants.MapPieceWidth + i * 3 + Constants.MapPieceWidth / 2;
						q.add(new Rect(x1, y1, x1 + 10, y1 + 10));
					}
				}
			}
		}
	}

	
	public void fire(Point[] p, boolean[] isExploded){
		if(p[0] != null){
			this.p1 = p[0];
			this.is1Exploded = isExploded[0];
			makeMovingAnimation(p1, q1);
			if(is1Exploded)
				lowerExplodeAnimation = new Animation(explodeTexture, new Point(20,28), new Point(15, 1), p1);
			else
				lowerExplodeAnimation = new Animation(splashTexture, new Point(40,40), new Point(24, 1), p1);
		}
		
		if(p[1] != null){
			this.p2 = p[1];
			this.is2Exploded = isExploded[1];
			makeMovingAnimation(p2, q2);
			if(is2Exploded)
				upperExplodeAnimation = new Animation(explodeTexture, new Point(20, 28), new Point(15,1), p2);
			else
				upperExplodeAnimation = new Animation(splashTexture, new Point(40, 40), new Point(24, 1), p2);
		}
		isInitialized = true;
	}
	
	public void makeLowerExplode(){
		if(lowerExplodeAnimation != null)
			lowerExplodeAnimation.PlayFromFrameIndex(0);
	}
	
	public void makeUpperExplode(){
		if(upperExplodeAnimation != null)
			upperExplodeAnimation.PlayFromFrameIndex(0);
	}
	
	public void update(){
		if (lowerExplodeAnimation != null)
        {
            lowerExplodeAnimation.update();
        }

        if (upperExplodeAnimation != null)
        {
            upperExplodeAnimation.update();
        }   
	}
	
	public void draw(Graphics g){
		if(q1.size() > 0){
			Rect dest = q1.poll();
			g.drawBitmap(Texture, dest.left, dest.top);
		}
		else if(lowerExplodeAnimation != null && lowerExplodeAnimation.IsActive)
			lowerExplodeAnimation.draw(g);
		
		if(q2.size() > 0){
			Rect dest = q2.poll();
			g.drawBitmap(Texture, dest.left, dest.top);
		}
		else if(upperExplodeAnimation != null && upperExplodeAnimation.IsActive)
			upperExplodeAnimation.draw(g);
	}
}
